<template>
	<view>
		<image class="bg" mode="widthFix" :src="$staticImagePath('common/top_bg.png')" />

		<navigationBar :shopInfo="shopInfo" :isStore="isStore" :locationInfo="locationInfo" />

		<view class="wrap flex column" v-if="dataInfo">
			<navInfo :redBagImg="dataInfo.bonus_image" :categoryList="dataInfo.cate_list" />

			<options
				:score_img="dataInfo.score_img"
				:specialInfo="dataInfo.special_list"
				:shopInfo="dataInfo.shop_list"
				:preferredInfo="dataInfo.preferred_list"
				:discountInfo="dataInfo.discount_list"
				:travelInfo="dataInfo.travel_list"
			/>

			<goodsList :list="list" />
		</view>

		<publish :isShowArrow="isShowArrow" @onUpArrow="onUpArrow" />
	</view>
</template>

<script>
	import { getIndex, GetIndexList } from "@/API/home.js";
	import navigationBar from "@/components/navigationBar/homeNavigationBar.vue";
	import navInfo from "@/components/home/redbagNav.vue";
	import options from "@/components/home/options.vue";
	import goodsList from "@/components/home/goodsList.vue";
	import publish from "@/components/common/publish.vue";
	import event from "@/utils/event.js";

	import Location from "@/utils/location.js";
	export default {
		components: {
			navigationBar,
			navInfo,
			options,
			goodsList,
			publish,
		},
		data() {
			return {
				locationInfo: {
					status: "loading",
				}, //定位信息
				isStore: false, //是否是商家
				shopInfo: null, //店铺信息

				page: 1, //分页
				totalList: [], //总数据
				list: [],

				dataInfo: null,
				
				isShowArrow: false, //是否显示箭头
			};
		},
		onReady() {
			event.on("onSureChooseCity", this, (res) => {
				this.locationInfo = res;
				
				this.$nextTick(() => {
					this.onUpArrow()
					
					this.getData()
				})
			});
		},
		onShow() {
			if (this.$filters.loginStatus() === true) {
				this.$commonNetwork.getUserIdenInfoData((res) => {
					if (res.code === 1) {
						this.isStore = true;
						this.shopInfo = res.shop_id;
					} else {
						this.isStore = false;
						this.shopInfo = null;
					}
				});
			}
		},
		onLoad(options) {
			if (options.scene) {
				var scene = decodeURIComponent(options.scene);
				var sceneArr = scene.split("=");
				var pid = sceneArr[1];
				
				this.handlePid(pid);
			}
			if (options.pid) {
				this.handlePid(options.pid);
			}

			this.onLocation();
		},
		onUnload() {
			event.remove("onSureChooseCity", this);
		},
		onPageScroll(e) {
			if (e.scrollTop > 300) {
				this.isShowArrow = true;
			} else {
				this.isShowArrow = false;
			}
		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh();
		},
		/**
		 * 加载更多
		 */
		onReachBottom: function() {
			if (this.list.length < this.totalList.length) {
				this.page++
				this.onShowRecommendData()
			}
		},
		methods: {
			/**
			 * 处理上级ID
			 */
			handlePid(pid) {
				this.$stoarge.pid = pid;
				
				if (this.$filters.loginStatus() === true) {
					this.$commonNetwork.onBindHigher();
				}
			},
			/**
			 * 定位
			 */
			onLocation() {
				Location.getLocation((obj) => {
					this.locationInfo = obj;
					
					this.$stoarge.setChangeCity(obj);
					
					this.getData();
				});
			},
			/**
			 * 向上箭头
			 */
			onUpArrow() {
				uni.pageScrollTo({
					scrollTop: 0
				})
			},
			/**
			 * 网络请求
			 */
			getData() {
				this.getHomeData();
				this.onRefresh();
			},
			/**
			 * 网络请求
			 */
			getHomeData() {
				let area = [this.locationInfo.province, this.locationInfo.city, this.locationInfo.district].join("/");
				getIndex(this.locationInfo.lat, this.locationInfo.lng, area).then((res) => {
					this.dataInfo = res.data;
				});
			},
			/**
			 * 刷新
			 */
			onRefresh() {
				this.page = 1;
				this.list = [];
				this.totalList = [];
				this.getListData();
			},
			/**
			 * 网络请求
			 */
			getListData() {
				let area = [this.locationInfo.province, this.locationInfo.city, this.locationInfo.district].join("/");
				GetIndexList({
					lat: this.locationInfo.lat,
					lng: this.locationInfo.lng,
					city: area,
				}).then((res) => {
					this.totalList = res.data;
					this.onShowRecommendData();
				});
			},
			/**
			 * 分页数据
			 */
			onShowRecommendData() {
				let start_num = (this.page - 1) * 20;
				let num = 20;
				if ((this.totalList.length - start_num) < num) {
					num = this.totalList.length - start_num;
				}
				const list = this.totalList.slice(start_num, start_num + num);
				if (this.page === 1) {
					this.list = list;
				} else {
					this.list = this.list.concat(list);
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.bg {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		z-index: -1;
	}

	.wrap {
		gap: 20rpx;
		padding-bottom: 20rpx;
	}
</style>
